[python tests] Fix determine-reboot-cause_test.py importing module sonic_platform#359
Open
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
Conversation
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
e12b57a to
78c6d46
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
oleksandrivantsiv
approved these changes
Mar 17, 2026
…nic_platform
This test, `determine-reboot-cause_test.py` and the script it tests,
`scripts/determine-reboot-cause` expect the module `sonic_platform` to not exist
on the system during unit tests on build hosts. The script implementation
handles this situation by trying to import `sonic_platform` in the function
calls, and having them return fake values in an `ImportError` exception handler.
Otherwise, if `sonic_platform` is present in the build system when running unit
tests, it will throw exceptions because the platform code is designed to run an
a real SONiC device host.
However, on the build system, there are races in the build, and it is possible
for `sonic_platform` to be installed and loaded. This will break this test.
This PR adds a fixture to remove the module and block it using a temporary
monkeypatch. `None` is assigned to `system.modules['sonic_platform']` to evict
any cached loaded modules and block future loading, until the test is complete.
This PR is in response to the following unit test failures appearing during
builds on some occasions:
```
FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_find_hardware_reboot_cause_not_installed_or_not_implemented - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_without_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
FAILED tests/determine-reboot-cause_test.py::TestDetermineRebootCause::test_determine_reboot_cause_main_with_reboot_cause_dir - TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
```
With details similar to:
```
_ TestDetermineRebootCause.test_find_hardware_reboot_cause_not_installed_or_not_implemented _
self = <tests.determine-reboot-cause_test.TestDetermineRebootCause object at 0x7f9c4a940d50>
def test_find_hardware_reboot_cause_not_installed_or_not_implemented(self):
> result = determine_reboot_cause.find_hardware_reboot_cause()
tests/determine-reboot-cause_test.py:140:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
scripts/determine-reboot-cause:127: in find_hardware_reboot_cause
hardware_reboot_cause_major, hardware_reboot_cause_minor = get_reboot_cause_from_platform()
scripts/determine-reboot-cause:115: in get_reboot_cause_from_platform
platform = sonic_platform.platform.Platform()
/usr/local/lib/python3.13/dist-packages/sonic_platform/platform.py:34: in __init__
if DeviceDataManager.get_dpu_count():
/usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
method.return_value = method(*args, **kwargs)
/usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:372: in get_dpu_count
dpu_data = cls.get_platform_dpus_data()
/usr/local/lib/python3.13/dist-packages/sonic_platform/utils.py:183: in _impl
method.return_value = method(*args, **kwargs)
/usr/local/lib/python3.13/dist-packages/sonic_platform/device_data.py:357: in get_platform_dpus_data
platform_path = device_info.get_path_to_platform_dir()
/usr/local/lib/python3.13/dist-packages/sonic_py_common/device_info.py:283: in get_path_to_platform_dir
platform_path_host = os.path.join(HOST_DEVICE_PATH, platform)
<frozen posixpath>:90: in join
???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
funcname = 'join', args = ('/usr/share/sonic/device', None), hasstr = True
hasbytes = False, s = None
> ???
E TypeError: join() argument must be str, bytes, or os.PathLike object, not 'NoneType'
<frozen genericpath>:188: TypeError
```
Signed-off-by: Judson Wilson <judsonw@nvidia.com>
78c6d46 to
de46066
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This test,
determine-reboot-cause_test.pyand the script it tests,scripts/determine-reboot-causeexpect the modulesonic_platformto not exist on the system during unit tests on build hosts. The script implementation handles this situation by trying to importsonic_platformin the funciton calls, and having them return fake values in anImportErrorexception handler. Otherwise, ifsonic_platformis present in the build system when running unit tests, it will throw exceptions because the platform code is designed to run an a real SONiC device host.However, on the build system, there are races in the build, and it is possible for
sonic_platformto be installed and loaded. This will break this test.This PR adds a fixture to remove the module and block it using temporary monkeypatching.
Noneis assigned tosystem.modules['sonic_platform']to evict any cached loaded modules and block future loading, until the test is complete.This PR is in response to the following unit test failures appearing during builds on some occasions:
With details similar to: